import json
import random


# 拼接多个排课id
def getCourseIds(rs):
    course_list = []
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    for l in rs_list:
        courseId = l["courseId"]
        course_list.append(courseId)
    return course_list


# 根据课程名称获取课程id
def getCurriculumId(rs, curriculumName):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    for l in rs_list:
        if l["curriculumName"] == curriculumName:
            curriculum_id = l["id"]
    return curriculum_id


# 随机获取教室id
def getRoomId(rs):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    room_id_list = []
    
    for l in rs_list:
        room_id = l["id"]
        room_id_list.append(room_id)
    return random.choice(room_id_list)


# 检查某个key是否包含对应的值
def checkValue(rs, key, values):
    if isinstance(rs, str):
        rs_list = json.loads(rs)
    else:
        rs_list = rs
    flag = 0
    if isinstance(values, str):
        value_list = json.loads(values)
    else:
        value_list = values
    for l in rs_list:
        for ct in value_list:
            if str(l[key]) == ct:
                flag = 1
                break
    return str(flag)